package com.data.modules.sm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.modules.sm.entity.SmCommunity;
import com.data.modules.sm.service.ISmCommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 时迈社区表
 * @Author: data-boot
 * @Date:   2022-11-11
 * @Version: V1.0
 */
@Api(tags="时迈社区表")
@RestController
@RequestMapping("/sm/smCommunity")
@Slf4j
public class SmCommunityController extends BootController<SmCommunity, ISmCommunityService> {
	@Autowired
	private ISmCommunityService smCommunityService;

	/**
	 * 分页列表查询
	 *
	 * @param smCommunity
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@ApiOperation(value="时迈社区表-分页列表查询", notes="时迈社区表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SmCommunity smCommunity,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SmCommunity> queryWrapper = QueryGenerator.initQueryWrapper(smCommunity, req.getParameterMap());
		Page<SmCommunity> page = new Page<SmCommunity>(pageNo, pageSize);
		IPage<SmCommunity> pageList = smCommunityService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @ApiOperation(value="查询全国地图数据", notes="查询全国地图数据")
	 @PostMapping(value = "/queryAllCityCommunityInfo")
	 public Result<?> queryAllCityCommunityInfo(SmCommunity smCommunity){

		 String area = "";
		 List<Map> list =  smCommunityService.getAllCommunityInfo(area);

		 return Result.OK(list);
	 }

	@ApiOperation(value="查询接入全国系统社区信息", notes="查询接入全国系统社区信息")
	@PostMapping(value = "/queryCommunityInfo")
	public Result<?> queryAllCommunityInfo(SmCommunity smCommunity){

		List<Map> list =  smCommunityService.getCommunityInfo(smCommunity.getProvince());

		return Result.OK(list);
	}




	/**
	 *   添加
	 *
	 * @param smCommunity
	 * @return
	 */
	@ApiOperation(value="时迈社区表-添加", notes="时迈社区表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SmCommunity smCommunity) {
		smCommunityService.save(smCommunity);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param smCommunity
	 * @return
	 */
	@ApiOperation(value="时迈社区表-编辑", notes="时迈社区表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SmCommunity smCommunity) {
		smCommunityService.updateById(smCommunity);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	//@ApiOperation(value="时迈社区表-通过id删除", notes="时迈社区表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		smCommunityService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	//@ApiOperation(value="时迈社区表-批量删除", notes="时迈社区表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.smCommunityService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@ApiOperation(value="时迈社区表-通过id查询", notes="时迈社区表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SmCommunity smCommunity = smCommunityService.getById(id);
		if(smCommunity==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(smCommunity);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param smCommunity
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmCommunity smCommunity) {
        return super.exportXls(request, smCommunity, SmCommunity.class, "时迈社区表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmCommunity.class);
    }

}
